package 每日一题;

import com.alibaba.fastjson.JSON;
import javafx.util.Pair;

import java.util.Map;
import java.util.TreeMap;

/**
 * @description:
 * @author: 小白白
 * @create: 2021-10-09
 **/

public class No352将数据流变为多个不相交区间 {

    static class SummaryRanges {

        private TreeMap<Integer, Pair<Integer,Integer>> treeMap = new TreeMap<>();

        public SummaryRanges() {

        }

        public void addNum(int val) {
            Map.Entry<Integer, Pair<Integer, Integer>> maxEntry = treeMap.ceilingEntry(val);
            Map.Entry<Integer, Pair<Integer, Integer>> minEntry = treeMap.floorEntry(val);

            if (maxEntry == null && minEntry == null) {
                treeMap.put(val, new Pair<>(val, val));
                return;
            }

            if (maxEntry != null && minEntry != null) {
                Pair<Integer, Integer> minEntryValue = minEntry.getValue();
                Integer n1 = minEntryValue.getKey();
                Integer n2 = minEntryValue.getValue();
                Pair<Integer, Integer> maxEntryValue = maxEntry.getValue();
                Integer n3 = maxEntryValue.getKey();
                Integer n4 = maxEntryValue.getValue();
                if (n1 == val || n2 == val || n3 == val || n4 == val) {
                    return;
                }
                if ((n1 < val && n2 > val) || (n3 < val && n4 > val)) {
                    return;
                }
                if (n2 + 1 == val && n3 - 1 == val) {
                    treeMap.remove(n1);
                    treeMap.remove(n3);
                    Pair<Integer,Integer> pair = new Pair<>(n1 ,n4);
                    treeMap.put(n1,pair);
                    return;
                }
                if (n2 + 1 == val) {
                    Pair<Integer,Integer> pair = new Pair<>(n1, val);
                    treeMap.put(n1, pair);
                    return;
                }
                if (n3 - 1 == val) {
                    Pair<Integer,Integer> pair = new Pair<>(val, n4);
                    treeMap.remove(n3);
                    treeMap.put(val, pair);
                    return;
                }
                treeMap.put(val, new Pair<>(val, val));
                return;
            }
            if (maxEntry != null && (maxEntry.getKey() - 1) == val) {
                Pair<Integer, Integer> pair = new Pair<>(val, maxEntry.getValue().getValue());
                treeMap.remove(maxEntry.getKey());
                treeMap.put(val, pair);
                return;
            }
            if (minEntry != null) {
                Pair<Integer, Integer> minEntryValue = minEntry.getValue();
                Integer n1 = minEntryValue.getKey();
                Integer n2 = minEntryValue.getValue();
                if ((n1 == val || n2 == val) || (n1 < val && n2 > val)) {
                    return;
                }
                if (n2 + 1 == val) {
                    treeMap.put(n1, new Pair<>(n1, val));
                    return;
                }
                treeMap.put(val, new Pair<>(val, val));
                return;
            }
            treeMap.put(val, new Pair<>(val, val));
        }

        public int[][] getIntervals() {
            int[][] arr = new int[treeMap.size()][2];
            int index = 0;
            for (Map.Entry<Integer, Pair<Integer, Integer>> entry : treeMap.entrySet()) {
                arr[index][0] = entry.getValue().getKey();
                arr[index][1] = entry.getValue().getValue();
                index++;
            }
            return arr;
        }

    }

    public static void main(String[] args) {
        SummaryRanges n = new SummaryRanges();
        n.addNum(6);
        n.addNum(6);
        n.addNum(0);
        n.addNum(4);
        n.addNum(8);
        n.addNum(7);
        n.addNum(6);
        n.addNum(4);
        n.addNum(7);
        n.addNum(5);
        System.out.println(JSON.toJSONString(n.getIntervals()));
    }

}
